﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.IO;

namespace tmAsk.Webs
{
    public partial class B_video : System.Web.UI.Page
    {
        string message = "<div>视频地址无效或此视频暂不支持!</div>";

        protected void Page_Load(object sender, EventArgs e)
        {

            string str = "0";
            string eventArgument = Request.QueryString["url"];

            if (!string.IsNullOrEmpty(eventArgument))
            {
                int type = CheckUrlType(eventArgument);
                if (type == -1)
                {
                    Response.Write(message);
                    Response.End();
                }
                try
                {
                    WebClient client = new WebClient();
                    Stream data = client.OpenRead(eventArgument);
                    StreamReader reader = new StreamReader(data);
                    string s = reader.ReadToEnd();
                    switch (type)
                    {
                        case 0:
                            str = YouKu(s);
                            break;
                        case 1:
                            str = Ku6(s);
                            break;
                        case 2:
                            str = W56(eventArgument);
                            break;
                        default:
                            break;
                    }
                    Response.Write(str);
                    data.Close();
                    // reader.Close();
                }
                catch
                {
                    Response.Write(message);
                    Response.End();
                }

            }
            else
            {
                Response.Write(message);
            }
        }

        private string Ku6(string s)
        {
            s = s.Substring(s.IndexOf("<input id=\"outSideSwfCode\" type=\"text\" readonly=\"readonly\" class=\"input\" value=\""));
            s = s.Replace("<input id=\"outSideSwfCode\" type=\"text\" readonly=\"readonly\" class=\"input\" value=\"", "");
            s = s.Substring(0, s.IndexOf("\""));
            if (string.IsNullOrEmpty(s))
            {
                return message;
            }
            s = "<embed src=\"" + s + "\" width=\"610\" height=\"498\">";
            return s;
        }
        private string YouKu(string s)
        {
            s = s.Substring(s.IndexOf("<input type=\"text\" id=\"link2\" value=\""));
            s = s.Replace("<input type=\"text\" id=\"link2\" value=\"", "");
            s = s.Substring(0, s.IndexOf("\""));
            if (string.IsNullOrEmpty(s))
            {
                return message;
            }
            s = "<embed src=\"" + s + "\" width=\"610\" height=\"498\">";
            return s;
        }
        private string W56(string s)
        {
            s = s.Replace("http://www.56.com/", "");
            s = s.Replace(".html", ".swf");
            s = s.Substring(s.IndexOf("/"));
            s = "http://player.56.com" + s;
            s = "<embed src=\"" + s + "\" width=\"610\" height=\"498\">";
            return s;
        }

        private int CheckUrlType(string url)
        {
            string[] urlArr = { "http://v.youku.com", "http://v.ku6.com", "http://www.56.com/" };
            for (int i = 0; i < urlArr.Length; i++)
            {
                if (url.Contains(urlArr[i]))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}